package com.py.base.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.py.base.entity.I18nInfo;
import com.py.base.entity.I18nInfoExcel;
import com.py.base.service.I18nInfoService;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
public class ImportExcelController {
	@Autowired
	private I18nInfoService i18nInfoService;
	
	@PostMapping("/importExcel")
	public String importExcel2(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException{ 
	    String serviceName  = request.getParameter("serviceName"); 
	    String moduleName   = request.getParameter("moduleName"); 
	    String functionName = request.getParameter("functionName"); 
	    ImportParams params = new ImportParams();
	    log.info("=serviceName:"+serviceName+"=moduleName:"+moduleName+"==functionName:"+functionName);

	    try {
	        List<I18nInfoExcel> list = ExcelImportUtil.importExcel(
                    file.getInputStream(),
                    I18nInfoExcel.class, params);
	        log.info(JSON.toJSONString(list));
	        
	        List<I18nInfo> iList =new ArrayList();
	        
	        for(I18nInfoExcel i : list){
	        	 
	        	String key  = i.getKey();
	        	
	        	String  zh  = i.getZh();
	        	if(!StringUtils.isEmpty(zh) ){
	        		  setI18nInfoValue(zh,iList,key,
	        				  "ZH",  
	        				  serviceName,
	        				  moduleName,
	        				  functionName);
	        	
	        	}
	        	String  en  = i.getEn();
	        	if(!StringUtils.isEmpty(en) ){
	        		setI18nInfoValue(en,iList,key,
	        				  "EN",  
	        				  serviceName,
	        				  moduleName,
	        				  functionName);
	        	}
	        	String  ja  = i.getJa();
	        	if(!StringUtils.isEmpty(ja) ){
	        		setI18nInfoValue(ja,iList,key,
	        				  "JA",  
	        				  serviceName,
	        				  moduleName,
	        				  functionName);
	        	}
	        	String  ko  = i.getKo();
	        	if(!StringUtils.isEmpty(ko) ){
	        		setI18nInfoValue(ko,iList,key,
	        				  "KO",  
	        				  serviceName,
	        				  moduleName,
	        				  functionName);
	        	}
	        	String  th  = i.getTh();
	        	if(!StringUtils.isEmpty(th) ){
	        		setI18nInfoValue(th,iList,key,
	        				  "TH",  
	        				  serviceName,
	        				  moduleName,
	        				  functionName);
	        	}
	        }
	        i18nInfoService.insertCodeBatch(iList);
	         for(I18nInfo i:iList){
	        	 log.info(JSON.toJSONString(i));
	         }
	    } catch (IOException e) {
	    	e.printStackTrace();
	        log.error("导入IO失败1：{}", e.getMessage());
	    } catch (Exception e1) {
	    	e1.printStackTrace();
	        log.error("导入失败2：{}", e1.getMessage());
	    }
	    return "chenggong";
	}
	private void setI18nInfoValue(
			String value, List<I18nInfo> iList ,String key,
			String language,
			 String serviceName,
		    String moduleName,
		    String functionName
			){
		I18nInfo i18nInfo = new I18nInfo();
		i18nInfo.setKey(key);
		i18nInfo.setValue(value);
		i18nInfo.setServiceName(serviceName);
		i18nInfo.setModuleName(moduleName);
		i18nInfo.setFunctionName(functionName);
		i18nInfo.setLanguage(language);
		iList.add(i18nInfo);
	}
 
}
